<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/static/common/default.jsp"%>
<!DOCTYPE html>
<html lang="en">
	<head>
	<jsp:include page="/style/header.jsp"></jsp:include>
	</head>
	
	<body>
	<div class="title_bar">
        <h3>零星机械申请</h3>
        <a href="javascript:history.go(-1);" class="go_back"></a>
    </div>
    <div class="apply">
    <form	id="mechanicalApply" method="post">
        <div class="ipt_list">
            <div class="ipt_cell">
                <span class="tag">里程桩号</span>
                <input type="text" placeholder="请输入里程桩号" id="mileageCode" name="mileageCode">
            </div>
            <div class="ipt_cell">
                <span class="tag">机械申请内容</span>
                <input type="text" placeholder="请输入用工内容" id="applyContent" name="applyContent">
            </div>
            <div class="ipt_cell">
                <span class="tag">工程类别</span>
                <input type="text" class="slt" value="" placeholder="请选择工程类别" id="gclb" name="projectId">
            </div>
            <div class="ipt_cell">
                <span class="tag">工段名称</span>
                <input type="text" class="slt" value="" placeholder="请选择所属工段" id="gdmc" name="segmentId">
            </div>
            <div class="ipt_cell">
                <span class="tag">机械类型</span>
                <input type="text" class="slt" value="" placeholder="请选择机械类型" id="jxlx" name="jxlx">
            </div>
            <div class="ipt_cell">
                <span class="tag">机械型号</span>
                <input type="text" class="slt" value="" placeholder="请选择机械型号" id="jxxh" name="jxxh">
            </div>
            
            <div class="ipt_cell">
                <span class="tag">使用开始时间</span>
                <input type="text" class="date start_date" placeholder="使用开始时间" id="startTime" name="startTime">
            </div>
            <div class="ipt_cell">
                <span class="tag">使用结束时间</span>
                <input type="text" class="date end_date" placeholder="结束时间不早于开始时间" id="endTime" name="endTime">
            </div>
            <div class="ipt_cell">
                <span class="tag">租用项目机械</span>
               	<input type="text" class="slt" placeholder="请选择是否租用" id="rent">
            </div>
            <div class="ipt_cell">
                <span class="tag">扣除时间</span>
                <input type="text"  placeholder="请输入扣除时间" id="deductHour" name="deductHour">
            </div>
            <div class="ipt_cell">
                <span class="tag">机械单价</span>
                <input type="text" placeholder="请选择机械单价" id="jxdj" name="price">
            </div>
            <div class="ipt_cell">
                <span class="tag">台数</span>
                <input type="text" placeholder="请输入台数" id="ts" name="quantity">
            </div>
            <div class="ipt_cell">
                <span class="tag">施工队</span>
                <input type="text" class="slt" value="" placeholder="请选择施工队" id="sgd" name="constructionId">
            </div>
            <div class="ipt_cell" style="border: none;">
                <span class="tag">附加照片</span>
                <div class="tip">前、中、后三张</div>
            </div>
            <div><input type="text" id="frontImage" name="frontImage" value="" style="display:none;"/><input type="text" id="middleImage" name="middleImage" value="" style="display:none;"/><input type="text" value="" id="behindImage" name="behindImage" style="display:none;"/></div>
            <input  type="file"  id="img1file" name="myfile" onchange="fileChange1(this)" style="display:none"/><input  type="file"  id="img2file" name="myfile" onchange="fileChange2(this)" style="display:none"/><input  type="file"  id="img3file" name="myfile" onchange="fileChange3(this)" style="display:none"/>
            <input type="file" id="uploadphoto" style="display:none;" />
            <div class="img_upload"><span class="add_img"><img src="${ctx}/style/images/img_upload.png" onclick="upload(1)" id="frontimg" alt=""></span><span class="add_img"><img src="${ctx}/style/images/img_upload.png" onclick="upload(2)" id="middleimg" alt=""></span><span class="add_img"><img src="${ctx}/style/images/img_upload.png" onclick="upload(3)" id="behindimg" alt=""></span></div>
            <!--<div class="img_upload" style="border-bottom: 1px solid #ccc; padding-bottom: 10px;"><span class="add_img"><input name="frontImage" id="frontImage" class="text-field" type="file" style="background-image: ${ctx}/style/images/img_upload.png" required="true" vtype="/.+(jpg|jpeg|png)$/" title="图片只支持jpg,jpeg和png格式"></span><span class="add_img"><input name="middleImage" id="middleImage" class="text-field" type="file" style="background-image: ${ctx}/style/images/img_upload.png" required="true" vtype="/.+(jpg|jpeg|png)$/" title="图片只支持jpg,jpeg和png格式"></span><span class="add_img"><input name="behindImage" id="behindImage" class="text-field" type="file" style="background-image: ${ctx}/style/images/img_upload.png" required="true" vtype="/.+(jpg|jpeg|png)$/" title="图片只支持jpg,jpeg和png格式"></span></div>-->
        </div>
        <div class="btn btn_sub">申 请</div>
    </form>
    </div>
	<jsp:include page="/style/footer.jsp"></jsp:include>
	<script src='${ctx}/style/upload/js/LocalResizeIMG.js'></script>
    <script src='${ctx}/style/upload/js/patch/mobileBUGFix.mini.js'></script>
    <script type="text/javascript" src='${ctx}/static/dist/lrz.bundle.js'></script>
	<script>
	function upload(num){
		var str = "img"+num+"file";
		$("#"+str).click();
	}
	
	
    function fileChange1(that){  
        var filepath=$(that).val();  
        if(filepath=="")  
        {  
            return;  
        }  
        var extStart=filepath.lastIndexOf(".");  
        var ext=filepath.substring(extStart,filepath.length).toUpperCase();  
        if(".jpg|.png|.bmp|.jpeg".toUpperCase().indexOf(ext.toUpperCase())==-1){  
        	alert("只允许上传jpg、png、bmp、jpeg格式的图片");  
            return false;  
        }  
     //以图片宽度为800进行压缩  
    lrz(that.files[0], {  
         width: 400  
       })  
    .then(function (rst) {  
            //压缩后异步上传  
            $.ajax({  
            url : "${ctx}/mechanicalApply/common/fileUploadPicture",  
            type: "POST",  
            data : {  
                imgdata1:rst.base64//压缩后的base值  
            },  
            dataType:"json",  
            cache:false,  
            async:false,  
            success : function(data) {
            	if(data.status==0){
            		var ct = "${ctx}"; 
            		$("#frontImage").attr("value",data.data);
            		$("#frontimg").attr("src","${ctx}"+data.data);
            		
            		//$('.img_upload .add_img.active img').attr('src', data.data);
            	}
                        },  
        error : function(){  
                alert("上传失败");  
                        }  
                    });  
         });  
}  
    function fileChange2(that){  
        var filepath=$(that).val();  
        if(filepath=="")  
        {  
            return;  
        }  
        var extStart=filepath.lastIndexOf(".");  
        var ext=filepath.substring(extStart,filepath.length).toUpperCase();  
        if(".jpg|.png|.bmp|.jpeg".toUpperCase().indexOf(ext.toUpperCase())==-1){  
           alert("只允许上传jpg、png、bmp、jpeg格式的图片");  
            return false;  
        }  
     //以图片宽度为800进行压缩  
    lrz(that.files[0], {  
         width: 400  
       })  
    .then(function (rst) {  
            //压缩后异步上传  
            $.ajax({  
            url : "${ctx}/mechanicalApply/common/fileUploadPicture",  
            type: "POST",  
            data : {  
                imgdata2:rst.base64//压缩后的base值  
            },  
            dataType:"json",  
            cache:false,  
            async:false,  
            success : function(data) {
            	if(data.status==0){
            		$("#middleImage").attr("value",data.data);
            		$("#middleimg").attr("src","${ctx}"+data.data);
            		
            		//$('.img_upload .add_img.active img').attr('src', data.data);
            	}
                        },  
        error : function(){  
                alert("上传失败");  
                        }  
                    });  
         });  
}  
    function fileChange3(that){  
        var filepath=$(that).val();  
        if(filepath=="")  
        {  
            return;  
        }  
        var extStart=filepath.lastIndexOf(".");  
        var ext=filepath.substring(extStart,filepath.length).toUpperCase();  
        if(".jpg|.png|.bmp|.jpeg".toUpperCase().indexOf(ext.toUpperCase())==-1){  
           alert("只允许上传jpg、png、bmp、jpeg格式的图片");  
            return false;  
        }  
     //以图片宽度为800进行压缩  
    lrz(that.files[0], {  
         width: 400  
       })  
    .then(function (rst) {  
            //压缩后异步上传  
            $.ajax({  
            url : "${ctx}/mechanicalApply/common/fileUploadPicture",  
            type: "POST",  
            data : {  
                imgdata3:rst.base64//压缩后的base值  
            },  
            dataType:"json",  
            cache:false,  
            async:false,  
            success : function(data) {
            	if(data.status==0){
            		$("#behindImage").attr("value",data.data);
            		$("#behindimg").attr("src","${ctx}"+data.data);
            		
            		//$('.img_upload .add_img.active img').attr('src', data.data);
            	}
                        },  
        error : function(){  
                alert("上传失败");  
                        }  
                    });  
         });  
}  
	
    var picker_rent = new mui.PopPicker();
    picker_rent.setData([{
        value: "1",
        text: "否"
    }, {
        value: "2",
        text: "是"
    }])
    
    $('#rent').click(function() {
    	picker_rent.show(function(SelectedItem) {
            $('#rent').val(SelectedItem[0].text);
            $('#rent').attr('data-value', SelectedItem[0].value);
        })
    });
    
	$(document).on('click', '.date', function() {
        var _this = $(this);
        var options = $(this).attr('data-options');
        var picker = new mui.DtPicker({
            type: "hour"
        });
        picker.show(function(rs) {
            _this.val(rs.text);
            picker.dispose();
        })
    });
	
	// gclb
    var picker_gclb = new mui.PopPicker();
    picker_gclb.setData([{
        value: "1",
        text: "路基"
    }, {
        value: "2",
        text: "路面"
    } , {
    	value: "3",
        text: "桥涵"
    } , {
    	value: "4",
        text: "隧道"
    } , {
    	value: "5",
        text: "其他"
    }])
    $('#gclb').click(function() {
        picker_gclb.show(function(SelectedItem) {
            $('#gclb').val(SelectedItem[0].text);
            $('#gclb').attr('data-value', SelectedItem[0].value);
        })
    });
    
	
    //机械类型
    var picker_jxlx = new mui.PopPicker();
    picker_jxlx.setData(${mechanicals});
    $('#jxlx').click(function() {
    	picker_jxlx.show(function(SelectedItem) {
    		$('#jxlx').val(SelectedItem[0].text);
    		$('#jxlx').attr('data-value', SelectedItem[0].value);
    		//机械型号
    		$.ajax({
    	        async: true,
    	        type:"POST",
    	        data:JSON.stringify({"mechanicalId": SelectedItem[0].value}),
    	        url: "${ctx}/mechanicalApply/listMechanicalByType?&mid="+Math.random(),
    	        dataType: "json",
    	        headers : {  
                    'Content-Type' : 'application/json;charset=utf-8'  
                },
                
                success : function(data) {
            		if (data != undefined && data.status != undefined
            				&& data.status.trim() == '0') {
            			picker_jxxh.setData(data.data);
            			result = true;
            		} else {
            			result = false;
            			tiper(data.error);
            		}
            	},
    		},"json")
    	});
    });
    
    //机械型号
    var picker_jxxh = new mui.PopPicker();
    $('#jxxh').click(function() {
    	picker_jxxh.show(function(SelectedItem) {
    		$('#jxxh').val(SelectedItem[0].text);
    		$('#jxxh').attr('data-value', SelectedItem[0].value);
    	});
    });
    
    //施工队
    var picker_sgd = new mui.PopPicker();
    picker_sgd.setData(${constructionteams});
    $('#sgd').click(function() {
        picker_sgd.show(function(SelectedItem) {
            $('#sgd').val(SelectedItem[0].text);
            $('#sgd').attr('data-value', SelectedItem[0].value);
        })
    });
    
    //所属工段
    var picker_gdmc = new mui.PopPicker();
    picker_gdmc.setData(${sectionitems});
    var secJson = ${defalutSecJson};
    picker_gdmc.pickers[0].setSelectedValue(secJson.value,secJson.text);
    $('#gdmc').click(function() {
        picker_gdmc.show(function(SelectedItem) {
            $('#gdmc').val(SelectedItem[0].text);
            $('#gdmc').attr('data-value', SelectedItem[0].value);
        })
    });
    
    //设置默认值
    $(document).ready(function(){
    	$('#gdmc').val(picker_gdmc.getSelectedItems()[0].text);
        $('#gdmc').attr('data-value', picker_gdmc.getSelectedItems()[0].value);
        $('#segmentId').val(picker_gdmc.getSelectedItems()[0].value);
        
        $('#rent').val(picker_rent.getSelectedItems()[0].text);
        $('#rent').attr('data-value', picker_rent.getSelectedItems()[0].value);
    })
    
     $('.btn_sub').click(function(e) {
    	var mileageCode = $('#mileageCode').val();
        var applyContent = $('#applyContent').val();
        var gclb = $('#gclb').attr("data-value");
        var jxlx = $('#jxlx').attr("data-value");
        var jxxh = $('#jxxh').attr("data-value");
        var sgd = $('#sgd').attr("data-value");
        var gdmc = $('#gdmc').attr("data-value");
        //var jxlx = $('#jxlx').val();
        //var jxxh = $('#jxxh').val();
        var startTime = $('#startTime').val();
        var endTime = $('#endTime').val();
        var jxdj = $('#jxdj').val();
        var ts = $('#ts').val();
        var constructionName = $('#sgd').val();
        var frontImage = $('#frontImage').val();
        var middleImage = $('#middleImage').val();
        var behindImage = $('#behindImage').val();
        var deductHour = $('#deductHour').val();
        var rent = $('#rent').attr("data-value");
        if(!mileageCode){
        	tiper('里程桩号不能为空');
            return false;
        }else if (!applyContent) {
            tiper('用工内容不能为空');
            return false;
        } else if (!gclb) {
            tiper('工程类别不能为空');
            return false;
        } else if (!jxlx) {
            tiper('机械类型不能为空');
            return false;
        }else if (!jxxh) {
        	tiper('机械型号不能为空');
            return false;
        }else if (!gdmc) {
        	tiper('所属工段不能为空');
            return false;
        }else if (!startTime) {
            tiper('开始时间不能为空');
            return false;
        } else if (!endTime) {
            tiper('结束时间不能为空');
            return false;
        } else if(!deductHour){
        	tiper('扣除时间不能为空');
            return false;
        }else if (!jxdj) {
            tiper('机械单价不能为空');
            return false;
        } else if (!ts) {
            tiper('台数不能为空');
            return false;
        } else if (!sgd) {
            tiper('施工队伍不能为空');
            return false;
        }else if(isNaN(jxdj)){
        	tiper('机械单价必须是数字');
        	return false;
        }else if(isNaN(ts)){
        	tiper('机械台数必须是数字');
        	return false;
        }else if(isNaN(deductHour)){
        	tiper('扣除时间必须是数字');
        	return false;
        }else {
        	 var apply_url = "${ctx}/mechanicalApply/save?&mid="+Math.random();
             var apply_data = {
             	 'mileageCode':mileageCode,
                 'applyContent': applyContent,
                 'projectId': gclb, 
                  //'mechanicalId':jxlx,
                 'mechanicalType':jxlx,
                 'mechanicalModel':jxxh,
                 'segmentId':gdmc,
                 'startTimeStr':startTime,
                 'endTimeStr':endTime,
                 'price':jxdj,
                 'quantity':ts,
                 'constructionId':sgd,
                 'constructionName':constructionName,
                 'deductHour':deductHour,
                 'rent':rent,
                 'applyUserId':"${currentUser.user_id}",
         		 'proId':"${currentUser.projectID}",
         		 //'segmentId':"${currentUser.segmentID}",
                 'frontImage':frontImage,
                 'middleImage':middleImage,
                 'behindImage':behindImage
             }
             myPost(apply_url,apply_data); 
        }
       
        
    })
    
    
    function myPost(reg_url,reg_data){
    	loading_show(); 
    	$.ajax({
	        async: true,
	        type:"POST",
	        data:JSON.stringify(reg_data),
	        url: "${ctx}/mechanicalApply/save?&mid="+Math.random(),
	        dataType: "json",
	        headers : {  
                'Content-Type' : 'application/json;charset=utf-8'  
            },
            success : function(data) {
            	loading_hide(500);
        		if (data != undefined && data.status != undefined
        				&& data.status.trim() == '0') {
        			result = true;
        			tiper('机械申请成功', 'green');
        			window.location.href = "${ctx}/mechanicalApply/toSuccess";
        		} else {
        			result = false;
        			tiper(data.error);
        		}
        	},
		},"json")
    }
     

	
	</script>
	</body>
	
</html>